﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using GameBase;
using Microsoft.Xna.Framework;

namespace GalaxyPresident
{
    public class MotherShip : Unit, CraftStation
    {
        private List<Unit> units = new List<Unit>();
        protected SendTask motherShipTask;
        float spaceSpeed = 0.5f;
        Vector3 spacePosition;

        public List<Unit> Units
        {
            get { return units; }
            set { units = value; }
        }

        public Vector3 SpacePosition
        {
            get { return spacePosition; }
            set { spacePosition = value; }
        }

        internal SendTask MotherShipTask
        {
            get { return motherShipTask; }
            set
            {
                motherShipTask = value;
                spacePosition = motherShipTask.Source.SolarSystem.Position;
            }
        }

        private static Texture2D texture = Constant.content.Load<Texture2D>(@"Resources/Unit/motherShip");
        private static Texture2D texture_s = Constant.content.Load<Texture2D>(@"Resources/Unit/motherShip_s");
        public MotherShip(Planet planet)
            : base(planet)
        {
            sprite_s = new Sprite(texture_s, 1, 1,
                   new List<int> { 0 }, 150);
            sprite = new Sprite(texture, 1, 1,
                new List<int> { 0 }, 150);
            _attackhelper = new AttackingHelper(new Bullet(this));
            _attackhelper.IsAutoChase = false;

            initSprites(planet);
        }
        public void AddUnit(Unit unit)
        {
            unit.Station = this;
            units.Add(unit);
        }
        public Unit RemoveUnit(Unit unit)
        {
            units.Remove(unit);
            return unit;
        }
        public Planet GetPlanet()
        {
            return null;
        }
        protected override Unit createInstance()
        {
            return new MotherShip(PlanetOwner);
        }
        public override void UpdateInfo(GameTime gameTime)
        {
            base.UpdateInfo(gameTime);
            foreach (Unit unit in units)
            {
                unit.UpdateInfo(gameTime);
            }
            if (motherShipTask != null)
            {
                if (Position == _targetMove && _planetOwner != motherShipTask.Target)
                {
                    if (parent != null)
                    {
                        
                        parent.RemoveChild(this);
                        _planetOwner = null;
                    }
                    else
                    {
                        Vector3 targetPosition = motherShipTask.Target.SolarSystem.Position;
                        if (targetPosition == spacePosition)/// Tới nơi
                        {
                            _planetOwner = motherShipTask.Target;
                            _planetOwner.AddToLayer(this);
                            moveTo(new Vector2(-motherShipTask.Target.Diameter * 10,
                                -motherShipTask.Target.Diameter * 10));
                            //motherShipTask = null;

                            motherShipTask.setCompleteSendTask();

                            if(motherShipTask.MotherShip.TeamOwner.TeamColor == PlayingScreen.PLAYER_COLOR
                                || (motherShipTask.MotherShip.PlanetOwner.TeamOwner != null && motherShipTask.MotherShip.PlanetOwner.TeamOwner.TeamColor == PlayingScreen.PLAYER_COLOR))
                                PlayingScreen.addNewPlanetMail(motherShipTask.Target);

                            if (motherShipTask.MotherShip.PlanetOwner.TeamOwner != null
                                && motherShipTask.MotherShip.PlanetOwner.TeamOwner == motherShipTask.MotherShip.TeamOwner)
                            {
                                this.sendAllResourceAndUnitToPlanet(motherShipTask.MotherShip.PlanetOwner);
                            }
                        }
                        else
                        {
                            spacePosition = MotionEffects.Move(spacePosition, targetPosition, spaceSpeed);
                        }
                    }

                }
                else if (Position == _targetMove && _planetOwner == motherShipTask.Target && TeamOwner == _planetOwner.TeamOwner)
                {
                    if (parent != null)
                    {
                        parent.RemoveChild(this);
                     //   _planetOwner = null;
                    }
                }

            }
        }

        private void sendAllResourceAndUnitToPlanet(Planet planet)
        {
            foreach (Unit unitzzz in units)
            {
                planet.AddUnit(unitzzz);
            }
            units.Clear();

            planet.Meterial += this.motherShipTask.SendingInfo.Material;
            planet.Energy += this.motherShipTask.SendingInfo.Enegry;

            this.motherShipTask.SendingInfo.Material = 0;
            this.motherShipTask.SendingInfo.Enegry = 0;
        }

        protected override void initInfo()
        {
            meterialCost = 2000;
            moneyCost = 3000;
            energyCost = 2000;

            _rangeAttack = 400;
            _healthPoint = 5000;
            _energyPoint = 1000;
            _speed = 10;
            _defense = 0.3f;

            Depth = 0.9f;
        }
        /*public Team GetTeamOwner()
        {
            return teamOwner;
        }*/
        public bool IsAlert(Vector2 armyPosition)
        {
            if ((armyPosition - position).Length() < 200)
            {
                return true;
            }
            return false;
        }
        public void Attack(PlanetObjectOwner obj)
        {
            foreach (Unit unit in units)
            {
                if (!unit.IsOn)
                {
                    unit.IsOn = true;
                    unit.Position = position;
                    _planetOwner.AddToLayer(unit);
                    unit.attackTo(obj);
                    return;
                }
                else
                {
                    unit.attackTo(obj);
                }
            }
        }
        public void Attack(Vector2 position)
        {
            foreach (Unit unit in units)
            {
                if (!unit.IsOn && _planetOwner != null)
                {
                    unit.IsOn = true;
                    unit.Position = position;
                    _planetOwner.AddToLayer(unit);
                    unit.moveTo(position);
                    return;
                }
                else
                {
                    unit.moveTo(position);
                }
            }
        }
        public void Withdraw()
        {
            foreach (Unit unit in units)
            {
                if (unit.IsOn)
                {
                    if (unit.Position == position)
                    {
                        unit.IsOn = false;
                        unit.Parent.RemoveChild(unit);
                    }
                    else
                    {
                        unit.moveTo(position);
                    }
                }
            }
        }
        public bool isEnoughResourceForBuildCapital()
        {
            if (motherShipTask.SendingInfo.Material >= 2500
                && motherShipTask.SendingInfo.Enegry >= 1200
                && motherShipTask.MotherShip.TeamOwner.TotalMoney >= 3000)
                return true;
            return false;
        }

        public void retreat()
        {
            Planet temp = motherShipTask.Source;
            motherShipTask.Source = motherShipTask.Target;
            motherShipTask.Target = temp;
            motherShipTask.Execute();
        }
    }
}